/**
 * Created with IntelliJ IDEA.
 * User: chenkaiyu
 * Date: 15-4-25
 * Time: 上午11:23
 * To change this template use File | Settings | File Templates.
 */

var grey = function(){
    function showBlockUI(message) {
        Metronic.blockUI({
            boxed: true,
            message: message
        });
        window.setTimeout(function () {
            Metronic.unblockUI();
        }, 500);
    };

    return{
        init:function(){
            $("#sio2").on("blur.save", function () {
                var $obj = $("#sio2");
                var sio2 = $obj.val();
                validate.init($obj);
                if (validate.isNotNumber(sio2) || sio2 > 100) {
                    validate.invalid();
                    return;
                }
                validate.valid();
                $.ajax({
                    url: basePath + "quality/grey/saveTheValueOfSiO2",
                    dataType:"json",
                    type:"post",
                    data: "sio2=" + sio2
                })
            })

            $("#al2o3").blur(function(){
                var $obj = $("#al2o3");
                var al2o3 = $obj.val();
                validate.init($obj);

                if(al2o3!=""){
                    if(validate.isNotNumber(al2o3) || al2o3 >100){
                        validate.invalid();
                        return;
                    }
                    validate.valid();
                    $.ajax({
                        url: basePath + "quality/grey/saveTheValueOfAl2O3",
                        dataType:"json",
                        type:"post",
                        data:"al2o3=" + al2o3
                    })
                } else{
                    validate.invalid();
                    return;
                }
            })

            $("#fe2o3").blur(function(){
                var $obj = $("#fe2o3");
                var fe2o3 = $obj.val();
                validate.init($obj);

                if(fe2o3!=""){
                    if(validate.isNotNumber(fe2o3) || fe2o3 > 100){
                        validate.invalid();
                        return;
                    }
                    validate.valid();
                    $.ajax({
                        url: basePath + "quality/grey/saveTheValueOfFe2o3",
                        dataType:"json",
                        type:"post",
                        data:"fe2o3=" + fe2o3
                    })
                }else{
                    validate.invalid();
                    return;
                }

            })

            $("#cao").blur(function(){
                var $obj = $("#cao");
                var cao = $obj.val();
                validate.init($obj);

                if(cao!="") {
                    if(validate.isNotNumber(cao) || cao >100){
                        validate.invalid();
                        return;
                    }
                    validate.valid();
                    $.ajax({
                        url: basePath + "quality/grey/saveTheValueOfCao",
                        dataType:"json",
                        type:"post",
                        data:"cao=" + cao
                    })
                } else{
                    validate.invalid();
                    return;
                }

            })

            $("#tio2").blur(function(){
                var $obj = $("#tio2");
                var tio2 = $obj.val();
                validate.init($obj);

                if(tio2!="") {
                    if(validate.isNotNumber(tio2) || tio2 >100){
                        validate.invalid();
                        return;
                    }
                    validate.valid();
                    $.ajax({
                        url: basePath + "quality/grey/saveTheValueOfTio2",
                        dataType:"json",
                        type:"post",
                        data:"tio2=" + tio2
                    })
                }else{
                    validate.invalid();
                    return;
                }

            })

            $("#mgo").blur(function(){
                var $obj = $("#mgo");
                var mgo = $obj.val();
                validate.init($obj);
                if(mgo!="")   {
                    if(validate.isNotNumber(mgo) || mgo >100){
                        validate.invalid();
                        return;
                    }
                    validate.valid();
                    $.ajax({
                        url: basePath + "quality/grey/saveTheValueOfMgo",
                        dataType:"json",
                        type:"post",
                        data:"mgo=" + mgo
                    })
                } else{
                    validate.invalid();
                    return;
                }
            })

            $("#so3").blur(function(){
                var $obj = $("#so3");
                var so3 = $obj.val();
                validate.init($obj);
                if(so3!="") {
                    if(validate.isNotNumber(so3) || so3 >100){
                        validate.invalid();
                        return;
                    }
                    validate.valid();
                    $.ajax({
                        url: basePath + "quality/grey/saveTheValueOfSo3",
                        dataType:"json",
                        type:"post",
                        data:"so3=" + so3
                    })
                }else{
                    validate.invalid();
                    return;
                }
            })

            $("#k2o").blur(function(){
                var $obj = $("#k2o");
                var k2o = $obj.val();
                validate.init($obj);
                if(k2o!="")   {
                    if(validate.isNotNumber(k2o) || k2o >100){
                        validate.invalid();
                        return;
                    }
                    validate.valid();
                    $.ajax({
                        url: basePath + "quality/grey/saveTheValueOfK2O",
                        dataType:"json",
                        type:"post",
                        data:"k2o=" + k2o
                    })
                }else{
                    validate.invalid();
                    return;
                }
            })

            $("#na2o").blur(function(){
                var $obj = $("#na2o");
                var na2o = $obj.val();
                validate.init($obj);
                if(na2o!="")   {
                    if(validate.isNotNumber(na2o) || na2o >100){
                        validate.invalid();
                        return;
                    }
                    validate.valid();
                    $.ajax({
                        url: basePath + "quality/grey/saveTheValueOfNa2o",
                        dataType:"json",
                        type:"post",
                        data:"na2o=" + na2o
                    })
                }else{
                    validate.invalid();
                    return;
                }
            })

            $("#other").blur(function(){
                var $obj = $("#other");
                var other = $obj.val();
                validate.init($obj);
                if(other!="")   {
                    if(validate.isNotNumber(other) || other >100){
                        validate.invalid();
                        return;
                    }
                    validate.valid();
                    $.ajax({
                        url: basePath + "quality/grey/saveTheValueOfOther",
                        dataType:"json",
                        type:"post",
                        data:"other=" + other,
                        success:function(data){
                            $("#other_show").html($("#other").val());
                        }
                    })
                }else{
                    validate.invalid();
                    return;
                }
            })

            $("#computer1").click(function(){
                var $obj = $("#other");
                var other = $obj.val();
                validate.init($obj);
                if(other ==""){
                    var grey = {
//                    "sio2":$("#sio2").val(),
//                    "al2o3":$("#al2o3").val(),
//                    "fe2o3":$("#fe2o3").val(),
//                    "cao":$("#cao").val(),
//                    "tio2":$("#tio2").val(),
//                    "mgo":$("#mgo").val(),
//                    "so3":$("#so3").val(),
//                    "k2o":$("#k2o").val(),
//                    "na2o":$("#na2o").val()
                    };
                    showBlockUI("计算中..");
                    validate.valid();
                    $.ajax({
                        url: basePath + "quality/grey/computer1",
                        contentType:"application/json; charset=utf-8",
                        data:JSON.stringify(grey),
                        dataType:"json",
                        type:"post",
                        success:function(data){
                            $("#other").val(data.returnMsg);
                            $("#other_show").html(data.returnMsg);
                        }
                    })
                }
            })

            $("#check").click(function(){
                    var grey = {
                        "sio2":$("#sio2").val(),
                        "al2o3":$("#al2o3").val(),
                        "fe2o3":$("#fe2o3").val(),
                        "cao":$("#cao").val(),
                        "tio2":$("#tio2").val(),
                        "mgo":$("#mgo").val(),
                        "so3":$("#so3").val(),
                        "k2o":$("#k2o").val(),
                        "na2o":$("#na2o").val(),
                        "other":$("#other").val()
                    };
                    showBlockUI("计算中..");
                    $.ajax({
                        url: basePath + "quality/grey/total",
                        contentType:"application/json; charset=utf-8",
                        data:JSON.stringify(grey),
                        dataType:"json",
                        type:"post",
                        success:function(data){
                            $("#total").val(data.returnMsg);
                            $("#total_show").html(data.returnMsg);
                        }
                    })

            })

            $("#dt").blur(function(){
                var $obj = $("#dt");
                var dt = $obj.val();
                validate.init($obj);
                if(dt!="")   {
                    if(validate.isNotNumber(dt)){
                        validate.invalid();
                        return;
                    }
                    validate.valid();
                    $.ajax({
                        url: basePath + "quality/grey/saveTheValueOfDt",
                        dataType:"json",
                        type:"post",
                        data:"dt=" + dt,
                        success:function() {
                            cal();
                        }
                    })
                } else{
                    validate.invalid();
                    return;
                }

                //如果变形温度DT和半球温度HT都有值，则自动计算结渣特性判断Rt

            });

            function cal() {
                var ht = $("#ht").val();
                var dt = $("#dt").val();
                if (validate.isNotNumber(dt) || validate.isNotNumber(ht))
                    return;
                $.ajax({
                    "url":"quality/grey/computer2",
                    type:"post",
                    dataType:"json",
                    success:function(data){
                        $("#rt").val(data.returnMsg);
                        $("#Rt_show").html(data.returnMsg);
                    }
                })
            }

            $("#st").blur(function(){
                var $obj = $("#st");
                var st = $obj.val();
                validate.init($obj);
                if(st!="") {
                    if(validate.isNotNumber(st)){
                        validate.invalid();
                        return;
                    }
                    validate.valid();
                    $.ajax({
                        url: basePath + "quality/grey/saveTheValueOfSt",
                        dataType:"json",
                        type:"post",
                        data:"st=" + st
                    })
                }else{
                    validate.invalid();
                    return;
                }
            })

            $("#ht").blur(function(){
                var $obj = $("#ht");
                var ht = $obj.val();
                validate.init($obj);
                if(ht!="" )   {
                    if(validate.isNotNumber(ht)){
                        validate.invalid();
                        return;
                    }
                    validate.valid();
                    $.ajax({
                        url: basePath + "quality/grey/saveTheValueOfHt",
                        dataType:"json",
                        type:"post",
                        data:"ht=" + ht,
                        success:function() {
                            cal();
                        }
                    })
                } else{
                    validate.invalid();
                    return;
                }

                //如果变形温度DT和半球温度HT都有值，则自动计算结渣特性判断Rt
                var dt = $("#dt").val();
                if (validate.isNotNumber(dt) || validate.isNotNumber(ht))
                    return;
                $.ajax({
                    "url":"quality/grey/computer2",
                    type:"post",
                    dataType:"json",
                    success:function(data){
                        $("#rt").val(data.returnMsg);
                        $("#Rt_show").html(data.returnMsg);
                    }
                })
            })

            $("#ft").blur(function(){
                var $obj = $("#ft");
                var ft = $obj.val();
                validate.init($obj);
                if(ft!="")   {
                    if(validate.isNotNumber(ft)){
                        validate.invalid();
                        return;
                    }
                    validate.valid();
                    $.ajax({
                        url: basePath + "quality/grey/saveTheValueOfFt",
                        dataType:"json",
                        type:"post",
                        data:"ft=" + ft
                    })
                }else{
                    validate.invalid();
                    return;
                }
            })

            $("#computer3").click(function(){
                var grey = {
                    "st":$("#st").val()
                }
                showBlockUI("计算中..");
                $.ajax({
                    "url":"quality/grey/computer3",
                    contentType:"application/json; charset=utf-8",
                    data:JSON.stringify(grey),
                    dataType:"json",
                    type:"post",
                    success:function(data){
                        $("#rz").val(data.returnMsg);
                        $("#Rz_show").html(data.returnMsg);
                    }
                })
            })

            $("#save").click(function(){
                window.location.href = basePath + "quality/grey/save";
            })
        },

        recover:function(){
            $.get(basePath + "quality/grey/recover",function(data){
                $("#sio2").val(getValidRecoverVal(data.sio2));
                $("#al2o3").val(getValidRecoverVal(data.al2o3));
                $("#fe2o3").val(getValidRecoverVal(data.fe2o3));
                $("#cao").val(getValidRecoverVal(data.cao));
                $("#tio2").val(getValidRecoverVal(data.tio2));
                $("#mgo").val(getValidRecoverVal(data.mgo));
                $("#so3").val(getValidRecoverVal(data.so3));
                $("#k2o").val(getValidRecoverVal(data.k2o));
                $("#na2o").val(getValidRecoverVal(data.na2o));
                $("#other").val(getValidRecoverVal(data.other));
                $("#total").val(getValidRecoverVal(data.total));
                $("#dt").val(getValidRecoverVal(data.dt));
                $("#st").val(getValidRecoverVal(data.st));
                $("#ht").val(getValidRecoverVal(data.ht));
                $("#ft").val(getValidRecoverVal(data.ft));
                $("#rt").val(getValidRecoverVal(data.rt));
                $("#rz").val(getValidRecoverVal(data.r2));

                $("#other_show").html(getValidRecoverVal(data.other));
                $("#total_show").html(getValidRecoverVal(data.total));
                $("#Rt_show").html(getValidRecoverVal(data.rt));
                $("#Rz_show").html(getValidRecoverVal(data.r2));
            });
        }
    }
}()
